package xvalidate

import (
	"gitee.com/xiedongji/antgo/cpts/xcfg"
	"gitee.com/xiedongji/antgo/helper/xutils"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"regexp"
	"strings"

	"github.com/go-playground/validator/v10"
)

/* ====================================================================== *
 * 功能:自定义验证规则
 * 描述:这里编写自定义的规则
 * ====================================================================== */
func IValidateRule(v *validator.Validate) {
	/* ====================================================================== *
	 * 描述:验证 int 类型的数据
	 * ====================================================================== */
	v.RegisterValidation("x_num", func(fl validator.FieldLevel) bool {
		return fl.Field().Int() > 0
	})

	/* ====================================================================== *
	 * 描述:验证数据表 唯一
	 * ====================================================================== */
	v.RegisterValidation("unique", func(fl validator.FieldLevel) bool {
		tableName := fl.Param()
		tablePrefix := xcfg.GetString("mysql.items.main.table_prefix")

		field := xutils.SnakeString(fl.StructFieldName())
		fieldVal := fl.Field().String()

		var userExists int64

		db, err := gorm.Open(mysql.Open(xcfg.GetString("mysql.items.main.dsn")), &gorm.Config{
			NamingStrategy: schema.NamingStrategy{
				TablePrefix:   "mc_",
				SingularTable: false,
			},
		})

		if err != nil {
			panic("i_rule 数据库连接报错")
		}

		db.Table(tablePrefix+tableName).Where(field, fieldVal).Count(&userExists)
		return userExists <= 0
	})

	// 从第三方 拷贝过来
	v.RegisterValidation("valid_service_name", func(fl validator.FieldLevel) bool {
		matched, _ := regexp.Match(`^[a-zA-Z0-9_]{6,128}$`, []byte(fl.Field().String()))
		return matched
	})
	v.RegisterValidation("valid_rule", func(fl validator.FieldLevel) bool {
		matched, _ := regexp.Match(`^\S+$`, []byte(fl.Field().String()))
		return matched
	})
	v.RegisterValidation("valid_url_rewrite", func(fl validator.FieldLevel) bool {
		if fl.Field().String() == "" {
			return true
		}
		for _, ms := range strings.Split(fl.Field().String(), ",") {
			if len(strings.Split(ms, " ")) != 2 {
				return false
			}
		}
		return true
	})
	v.RegisterValidation("valid_header_transfor", func(fl validator.FieldLevel) bool {
		if fl.Field().String() == "" {
			return true
		}
		for _, ms := range strings.Split(fl.Field().String(), ",") {
			if len(strings.Split(ms, " ")) != 3 {
				return false
			}
		}
		return true
	})
	v.RegisterValidation("valid_ipportlist", func(fl validator.FieldLevel) bool {
		for _, ms := range strings.Split(fl.Field().String(), ",") {
			if matched, _ := regexp.Match(`^\S+\:\d+$`, []byte(ms)); !matched {
				return false
			}
		}
		return true
	})
	v.RegisterValidation("valid_iplist", func(fl validator.FieldLevel) bool {
		if fl.Field().String() == "" {
			return true
		}
		for _, item := range strings.Split(fl.Field().String(), ",") {
			matched, _ := regexp.Match(`\S+`, []byte(item)) //ip_addr
			if !matched {
				return false
			}
		}
		return true
	})
	v.RegisterValidation("valid_weightlist", func(fl validator.FieldLevel) bool {
		for _, ms := range strings.Split(fl.Field().String(), ",") {
			if matched, _ := regexp.Match(`^\d+$`, []byte(ms)); !matched {
				return false
			}
		}
		return true
	})

}
